<template>
  <div class="recipe">
    <div class="top">菜谱分类</div>
    <van-search v-model="value" shape="round" @focus="toSearch" />
    <div class="loading" v-if="list.length == 0">
        <van-loading size="30px" color="#fbb705">加载中...</van-loading>
    </div>
    <div class="content" v-if="list.length != 0">
      <div class="content-left">
        <van-sidebar v-model="activeKey" @change="onChange">
          <van-sidebar-item v-for="(item,index) in list" :key="index" :title="item.name" />
        </van-sidebar>
      </div>
      <div class="content-right">
          <div class="content-right-item" v-for="(item1,index1) in cs" :key="index1">
              <div class="title" @click="toSearchInfo(item1.name)"><span>{{item1.name}}</span></div>
              <div class="info">
                  <div class="info-item" v-for="(item2,index2) in item1.cs" :key="index2" @click="toSearchInfo(item2.name)">
                      {{item2.name}}
                  </div>
              </div>
          </div>
      </div>
    </div>
  </div>
</template>

<script>
export default {
  name: "Recipe",
  data() {
    return {
      value:'',
      activeKey: 0,
      list:[],
      cs:[]
    };
  },
  created() {
    this.getData();
  },
  methods: {
     toSearch(){
      this.$router.push({name:'Search'})
    },
    getData() {
      this.axios({
        method: "get",
        url: "recipe/flatcatalogs",
      }).then((result) => {
          
        if(result.data.state == 'success'){
            result.data.result.cs.forEach(item=>{
                item.cs.forEach(ele=>{
                    if(ele.cs.length!=0){
                        this.list.push(item)
                    }
                })
            })
            let l = Array.from(new Set(this.list))
            this.list = l
        }
        let cs = []
        this.list[this.activeKey].cs.forEach(e=>{
            if(e.cs.length!=0){
                cs.push(e)
            }
        })
        this.cs = cs
      });
    },
    onChange(index){
        let cs = []
       this.list[index].cs.forEach(e=>{
            if(e.cs.length!=0){
                cs.push(e)
            }
        })
        this.cs = cs
    },
    toSearchInfo(name){
      this.$router.push({name:'SearchInfo',query:{value:name}})
    }
  },
};
</script>

<style lang="scss" scoped>
.recipe {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  .top{
    width: 100%;
    font-size: 18px;
    padding: 40px 20px 0;
    background-color: #fbb705;
    font-weight: 600;
  }
  .van-search{
      background-color: #fbb705;
    }
  .loading{
      width: 100%;
      height: calc(100% - 50px);
      display: flex;
      justify-content: center;
      align-items: center;
  }
  .content {
    width: 100%;
    height: calc(100% - 50px - 54px);
    display: flex;
    .content-left{
        width: 90px;
        overflow-y: auto;
        .van-sidebar{
            width: 100%;
        }
        .van-sidebar-item {
      background-color: rgb(240, 240, 240);
    }
    .van-sidebar-item--select,
    .van-sidebar-item--select:active {
      background-color: #fff;
    }
    .van-sidebar-item--select::before {
      position: absolute;
      top: 50%;
      left: 76px;
      height: 40%;
      background-color: #fbb705;
    }
    }
    .content-right{
        width: calc(100% - 90px);
        background-color: #fff;
        overflow-y: auto;
        padding: 10px 10px 50px 10px;
        .content-right-item{
            margin-bottom: 20px;
            .title{
                width: calc(100% - 40px);
                background-color: #fbb705;
                font-size: 20px;
                border-radius: 5px;
                padding: 5px 20px;
                color: #fff;
                margin-bottom: 5px;
                display: flex;
                justify-content: space-between;
            }
            .info{
                display: flex;
                flex-wrap: wrap;
                .info-item{
                    font-size: 15px;
                    border: 1px solid #000;
                    border-radius: 5px;
                    padding: 2px 10px;
                    margin: 5px;
                }
            }
        }
    }
  }
}
</style>